$('#username').change(function () {
    if ($(this).val().length < 6) {
        $(this).removeClass('is-valid');
        $(this).addClass('is-invalid');
    } else {
        $(this).removeClass('is-invalid');
        $(this).addClass('is-valid');
    }
});
$('#password').change(function () {
    if ($(this).val().length < 6) {
        $(this).removeClass('is-valid');
        $(this).addClass('is-invalid');
    } else {
        $(this).removeClass('is-invalid');
        $(this).addClass('is-valid');
    }
});
$('#rpassword').change(function () {
    if ($(this).val() != $('#password').val()) {
        $(this).removeClass('is-valid');
        $(this).addClass('is-invalid');
    } else {
        $(this).removeClass('is-invalid');
        $(this).addClass('is-valid');
    }
});
$('#email').change(function () {
    if (! (/.+@.+\..+/).test($(this).val())) {
        $(this).removeClass('is-valid');
        $(this).addClass('is-invalid');
    } else {
        $(this).removeClass('is-invalid');
        $(this).addClass('is-valid');
    }
});
$('#organization').change(function () {
    if ($(this).val() == '') {
        $(this).removeClass('is-valid');
        $(this).addClass('is-invalid');
    } else {
        $(this).removeClass('is-invalid');
        $(this).addClass('is-valid');
    }
});
$('#profile').change(function() {
    if (this.files.length > 0)
        $('#proimg')[0].src = URL.createObjectURL(this.files[0]);
});
let canvas = document.createElement('canvas');
canvas.width = canvas.height = 100;
$('#submit').click(function () {
    if ($('.is-valid').length == 5) {
        let img = $('#proimg')[0];
        canvas.getContext("2d").drawImage(img, 0, 0, 100, 100);
        $.ajax({
            type: 'post',
            url: 'register',
            data: {
                'username': $('#username').val(),
                'password': $('#password').val(),
                'email': $('#email').val(),
                'organization': $('#organization').val(),
                'education': $('#education').find('option:selected').text(),
                'working': sessionStorage.getItem('working_field'),
                'interested': sessionStorage.getItem('interested_field'),
                'profile': canvas.toDataURL('image/webp')
            },
            success: function(result, t) {
                if (result == 'success') location.href = '/register3.htm';
                else alert(result);
            },
            error: function (http, e) {
                alert(http.status + e);
            }
        });
    } else alert('Some value is valid.');
});
